// 上传页面JavaScript功能

// 显示上传状态
function showUploadStatus(message, type) {
    const statusDiv = document.getElementById('upload-status');
    statusDiv.textContent = message;
    statusDiv.className = `upload-status ${type}`;
    statusDiv.style.display = 'block';
    
    if (type === 'success') {
        setTimeout(() => {
            statusDiv.style.display = 'none';
        }, 3000);
    }
}

// 处理成员工时数据上传
document.getElementById('member-worktime-upload-form').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const fileInput = document.getElementById('member-worktime-file');
    const file = fileInput.files[0];
    
    if (!file) {
        showMessage('请选择文件', 'error');
        return;
    }
    
    const formData = new FormData();
    formData.append('file', file);
    formData.append('type', 'member_worktime');
    
    try {
        showUploadStatus('正在上传成员工时数据...', 'info');
        
        const response = await fetch('/api/upload', {
            method: 'POST',
            body: formData
        });
        
        const result = await response.json();
        
        if (response.ok) {
            showUploadStatus(`成功上传 ${result.count} 条成员工时数据`, 'success');
            fileInput.value = '';
        } else {
            showUploadStatus(result.error || '上传失败', 'error');
        }
    } catch (error) {
        console.error('上传错误:', error);
        showUploadStatus('上传失败，请重试', 'error');
    }
});

document.getElementById('member-worktime-file').addEventListener('change', function(e) {
    const label = document.querySelector('label[for="member-worktime-file"]');
    if (e.target.files.length > 0) {
        label.textContent = e.target.files[0].name;
    } else {
        label.textContent = '选择Excel文件';
    }
});

// 处理项目工时数据上传
document.getElementById('project-worktime-upload-form').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const fileInput = document.getElementById('project-worktime-file');
    const file = fileInput.files[0];
    
    if (!file) {
        showMessage('请选择文件', 'error');
        return;
    }
    
    const formData = new FormData();
    formData.append('file', file);
    formData.append('type', 'project_worktime');
    
    try {
        showUploadStatus('正在上传项目工时数据...', 'info');
        
        const response = await fetch('/api/upload', {
            method: 'POST',
            body: formData
        });
        
        const result = await response.json();
        
        if (response.ok) {
            showUploadStatus(`成功处理 ${result.count} 条工时数据`, 'success');
            fileInput.value = '';
        } else {
            showUploadStatus(result.error || '上传失败', 'error');
        }
    } catch (error) {
        console.error('上传错误:', error);
        showUploadStatus('上传失败，请重试', 'error');
    }
});

document.getElementById('project-worktime-file').addEventListener('change', function(e) {
    const label = document.querySelector('label[for="project-worktime-file"]');
    if (e.target.files.length > 0) {
        label.textContent = e.target.files[0].name;
    } else {
        label.textContent = '选择Excel文件';
    }
});